Skip to content

[release/10.0.1xx] [runtime] Avoid weak-reference deadlocks during fork. Fixes #26844 - #27051

Merged
rolfbjarne merged 1 commit into
release/10.0.1xxfrom
dev/rolf/backport-pr-26863-release/10.0.1xx-2026-10-27
Oct 28, 2026
Merged

rolfbjarne merged 1 commit into
release/10.0.1xxfrom
dev/rolf/backport-pr-26863-release/10.0.1xx-2026-10-27

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

Summary

Resolving an Objective-C weak reference takes a side-table lock before calling our retainWeakReference override. For dynamically registered types, reading the managed wrapper's finalization flags then calls objc_getAssociatedObject, which takes the association lock. Objective-C's fork preparation acquires those locks in the opposite order, so concurrent UI layout and Process.Start() can deadlock.

Cache NSObjectData alongside GC handles and flags in the existing native dictionary, so the weak-reference callback no longer needs the associated-object API. Preserve the cached data when a GC handle is temporarily cleared, including during copyWithZone, and remove the entry during native deallocation before its address can be reused. Debug logging on this path also avoids Objective-C class queries.

Deallocation cleanup checks the startup-configured dynamic-registration support flag before taking the dictionary mutex, avoiding unnecessary locking when dynamic registration is disabled without an unsynchronized dictionary-pointer read. NativeAOT excludes this cleanup at compile time.

Fixes #26844

🤖 Pull request created by Copilot


Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com
Co-authored-by: Rolf Bjarne Kvinge rokvin@microsoft.com
Co-authored-by: Copilot Autofix powered by AI 175728472+Copilot@users.noreply.github.com

Backport of #26863.

…26863)

Resolving an Objective-C weak reference takes a side-table lock before calling our `retainWeakReference` override. For dynamically registered types, reading the managed wrapper's finalization flags then calls `objc_getAssociatedObject`, which takes the association lock. Objective-C's fork preparation acquires those locks in the opposite order, so concurrent UI layout and `Process.Start()` can deadlock.

Cache `NSObjectData` alongside GC handles and flags in the existing native dictionary, so the weak-reference callback no longer needs the associated-object API. Preserve the cached data when a GC handle is temporarily cleared, including during `copyWithZone`, and remove the entry during native deallocation before its address can be reused. Debug logging on this path also avoids Objective-C class queries.

Deallocation cleanup checks the startup-configured dynamic-registration support flag before taking the dictionary mutex, avoiding unnecessary locking when dynamic registration is disabled without an unsynchronized dictionary-pointer read. NativeAOT excludes this cleanup at compile time.

Fixes #26844

🤖 Pull request created by Copilot

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Rolf Bjarne Kvinge <rokvin@microsoft.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@rolfbjarne
rolfbjarne requested a review from dalexsoto as a code owner October 27, 2026 07:44
@rolfbjarne rolfbjarne added backported ready-to-review This PR is ready to review/merge. labels Oct 27, 2026
Copilot AI balanced review requested due to automatic review settings October 27, 2026 07:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Native lock-order and cached NSObjectData lifetime changes require macOS dynamic-registrar stress validation of concurrent weak-reference resolution, fork safety, and deallocation, which is unavailable in this review environment.

No open issues 🎉

What changed in this PR

Backports the runtime fix for #26844 to release/10.0.1xx, addressing deadlocks when Objective-C weak-reference resolution overlaps with process creation.

Changes:

  • Cache NSObjectData alongside GC handles to avoid associated-object lookups during weak-reference callbacks.
  • Preserve cached data when GC handles are cleared, remove it during native deallocation, and avoid Objective-C class queries in affected debug logging.
  • Add regression tests for lock-independent lookup and cached-data lifecycle.
File Description
tests/​test-libraries/​libtest.m Adds a native helper to test data lookup while the association lock is held.
tests/​test-libraries/​libtest.h Declares the native regression-test helper.
tests/​monotouch-test/​ObjCRuntime/​NativeRuntimeTest.cs Tests lock-independent lookup, GC-handle updates, and deallocation cleanup.
runtime/​trampolines.m Implements cached data lookup, lifecycle cleanup, and safer debug logging.

Review effort: Balanced

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 057e1446a9c0938c88d4653b9e872be676b10657 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
rolfbjarne enabled auto-merge (squash) October 27, 2026 22:45
@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🚀 [CI Build #057e144] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 200 tests passed 🎉

Tests counts

✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker (iOS): All 15 tests passed. Html Report (VSDrops) Download
✅ linker (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ linker (macOS): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (tvOS): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 19 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 18 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 21 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 19 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Golden Gate (27): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

✅ Linux build succeeded

Pipeline on Agent
Hash: 057e1446a9c0938c88d4653b9e872be676b10657 [PR build]

@rolfbjarne
rolfbjarne merged commit 9260003 into release/10.0.1xx Oct 28, 2026
56 checks passed
@rolfbjarne
rolfbjarne deleted the dev/rolf/backport-pr-26863-release/10.0.1xx-2026-10-27 branch October 28, 2026 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backported ready-to-review This PR is ready to review/merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants